<?php

namespace app\job;

use app\model\Config;
use app\model\GoodsOrders;
use app\model\LineOrders;
use app\model\PointGoodsOrders;
use app\model\TrainOrders;
use app\model\TrainSchoolLevelVipTrainerClass;
use app\model\User;
use app\tool\Http;
use app\tool\Random;
use Exception;
use GuzzleHttp\Client;
use TCPDF;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\queue\Job;

class Task
{
	

	public function fire(Job $job, $data)
	{
		
		$rt = $this->doJob($data);
		if ($rt) {
			$job->delete();
			return true;
		}
		
		// 重试三次失败
			if ($job->attempts() >= 3) {
				$job->release(60*60*24);
				return false;
			}
		
		//执行失败10S后重试
		$job->release(10);
		
	}
	
	public function doJob($data)
	{
		try {
			if (array_key_exists('action', $data)) {
				//超时系统自动操作
				switch ($data['action']) {
					case 'line_order_timeout':
						//订单超时取消
						$order_id = $data['order_id'];
						$row = LineOrders::findOrFail($order_id);
						if ($row->status == 1) {
							$row->status = 5;
							$row->save();
						}
						break;
					case 'line_order_done':
						//到出行日期  订单自动完成
						$order_id = $data['order_id'];
						$row = LineOrders::findOrFail($order_id);
						if ($row->status == 2) {
							$row->status = 3;
							$row->save();
						}
						break;
					case 'vip_expire':
						$user_id = $data['user_id'];
						$user = User::findOrFail($user_id);
			
						if ($user->vip_expiretime <= time()) {
							//已过期
							$user->level = 1;
							$user->save();
						}
						break;
				}
			}
		} catch (ModelNotFoundException $e) {
			dump('找不到数据');
			return true;
		} catch (Exception $e) {
			dump($e->getMessage());
			dump($e->getLine());
			return false;
		}
		return true;
	}
	
	
}
